package com.bdqn.controller;

import com.bdqn.pojo.easyuser;
import com.bdqn.service.impl.easyUserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource(name = "easyUserServiceImpl")
    private easyUserServiceImpl userService;


    @RequestMapping("/user/login")
    public easyuser login(@RequestBody easyuser user){
        System.out.println(user.getUsername()+user.getPassword());
        easyuser easy = userService.login(user.getUsername(),user.getPassword());
        System.out.println(easy);
        return  easy;
    }


    @GetMapping("/user/findAllByUsername")
    public PageInfo<easyuser> findAllByUsername(String query ,Integer pageIndex ,Integer pageSize){
        //封装pagehelper的分页对象  参数 页面 页大小
        PageHelper.startPage(pageIndex,pageSize);
        List<easyuser> easyuserList = userService.findAllByUsername(query);
        //将集合存入到pageinfo对象中
        PageInfo<easyuser> pageInfo = new PageInfo<easyuser>(easyuserList);
        return pageInfo;
    }
    @RequestMapping("/user/deleteUserByid/{id}")
    public String deleteUserByid(@PathVariable  Integer id){
        int i = userService.deleteUserByid(id);
        String mess = "";
        if (i>0){
            mess = "yes";
        }else {
            mess = "no";
        }
        return  mess;
    }


    @RequestMapping("/user/saveUser")
    public String saveUser(@RequestBody easyuser user){
        System.out.println(user);
        int i = userService.saveUser(user);
        String mess = "";
        if (i>0){
            mess = "yes";
        }else {
            mess = "no";
        }
        return  mess;
    }
    @RequestMapping("/user/addUser")
    public String addUser(@RequestBody easyuser user){
        int i = userService.addUser(user);
        String mess = "";
        if (i>0){
            mess = "yes";
        }else {
            mess = "no";
        }
        return  mess;
    }

    @RequestMapping("/user/getUserByid/{id}")
    public easyuser getUserByid(@PathVariable Integer id){
        return userService.getUserByid(id);
    }

    @RequestMapping("/user/saveState")
    public String saveState(Integer id , boolean state){
        return userService.saveState(id, state)>0?"yes":"no";
    }

    @RequestMapping("/user/findRole")
    public List<String> findRole (){
        return userService.findRole();

    }




}
